/** 菜单分类 **/
var menu1=[],menu2=[],menu3=[];
var menu = session.getMenu();
for(var i=0;i<menu.length;i++){
	var mi = menu[i];
	if(mi['rsLevel'] == 1){
		menu1.push(mi);
	}else if(mi['rsLevel'] == 2){
		menu2.push(mi);
	}else if(mi['rsLevel'] == 3){
		menu3.push(mi);
	}
}
/****/

//封装返回的html，如：图片路径包装等
var wrapHtml = function(html){
	var obj = $('<div>' + html + '</html>');
	obj.find('img').each(function(i){
		var m = $(this);
		m.attr('src',config.client + m.attr('src'));
	});
	return obj.html();
}

//顶部
var page_top = $.get({ url:'assets/inc/page-top.html', type:'get', async:false}).responseText;
$('.page-container').before(wrapHtml(page_top));

//组装一级菜单
var _selected_system = sessionStorage['_selected_system'] || menu1[0].rsID;
for(var i=0;i<menu1.length;i++){
	var mi = menu1[i];
	var html = '';
	var active = '';
	if(_selected_system == mi.rsID) active = ' active';
	html += '<li mid="' + mi.rsID + '" class="menu-dropdown' + active + '" url="' + mi.rsURL + '">';
	html += '<a href="javascript:;"><i class="' + mi.iconClass + '"></i> ' + mi.rsName + ' </a>';
	html += '</li>';
	$('.hor-menu ul').append(html);
}

$('.hor-menu .menu-dropdown').each(function(i,m){
	$(m).attr('index',i);
	$(m).click(function(){
		var el = $(this);
		var url = el.attr('url');
		if(!url) return;
		if(el.hasClass('active')) return;
		
		$('.hor-menu .menu-dropdown').removeClass('active');
		el.addClass('active');
		
		var index = el.attr('mid');
		sessionStorage['_selected_system'] = index;
		
		location.href = config.client + 'home.html';
	});
});

//左侧
var page_left = $.get({ url:'assets/inc/page-left-0.html', type:'get', async:false}).responseText;
$('.page-container').prepend(page_left);

//组装二级菜单
for(var i=0;i<menu2.length;i++){
	var mi = menu2[i];
	if(mi.rsPID == _selected_system){
		var html = '';
		html += '<li class="nav-item  ">';
		html += '	<a href="javascript:;" class="nav-link nav-toggle">';
		html += '		<i class="' + mi.iconClass + '"></i>';
		html += '		<span class="title">' + mi.rsName + '</span>';
		html += '		<span class="arrow"></span>';
		html += '	</a>';
		html += '	<ul id="sub_menu' + mi.rsID + '" class="sub-menu">';
		html += '	</ul>';
		html += '</li>';
		$('.page-sidebar-menu').append(html);
	}
}
//组装三级菜单
for(var i=0;i<menu3.length;i++){
	var mi = menu3[i];
	var html = '';
	html += '<li class="nav-item">';
	html += '	<a href="' + mi.rsURL + '" class="nav-link ">';
	html += '		<i class="' + mi.iconClass + '"></i>';
	html += '		<span class="title">' + mi.rsName + '</span>';
	html += '	</a>';
	html += '</li>';
	$('#sub_menu' + mi.rsPID + '').append(html);
}
//右侧
//var page_right = $.get({ url:'assets/inc/page-right.html', type:'get', async:false}).responseText;
//$('.page-container').append(page_right);

//底部
var page_bottom = $.get({ url:'assets/inc/page-bottom.html', type:'get', async:false}).responseText;
$('.page-container').after(page_bottom);